package com.summerli.service.impl;

import com.summerli.common.ResponseResult;
import com.summerli.common.sms.SmsSendRequest;
import com.summerli.common.sms.SmsTemplateDto;
import com.summerli.feign.SmsClient;
import com.summerli.service.ShortMsgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@Slf4j
public class ShortMsgServiceImpl implements ShortMsgService {

    @Autowired
    private SmsClient smsClient;

    @Override
    public ResponseResult send(String phoneNumber, String code) {
        // 调用三方服务，发送短信
        SmsSendRequest sendRequest = new SmsSendRequest();
        String[] receivers = new String[1];
        receivers[0] = phoneNumber;
        List<SmsTemplateDto> templateDtos = new ArrayList<>();
        SmsTemplateDto dto = new SmsTemplateDto();
        dto.setId("SMS_144145499");
        HashMap<String, Object> templateMap = new HashMap<>();
        templateMap.put("code", code);
        dto.setTemplateMap(templateMap);
        templateDtos.add(dto);
        sendRequest.setReceivers(receivers);
        sendRequest.setData(templateDtos);
        return smsClient.sendSms(sendRequest);
    }
}
